package net.keebook.webapp.ws;

import java.util.List;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import net.keebook.webapp.dao.PasswordDao;
import net.keebook.webapp.model.SessionModel;


@Path("/searchsuggest")
@Stateless
public class SearchSuggestService extends WebService {
	@Inject
	private PasswordDao passwordDao;
	
	@GET
	@Produces(MediaType.TEXT_PLAIN)
	public String saveSettings(@Context HttpServletRequest request, @DefaultValue("") @QueryParam("q") String keyword) {
		if ("".equals(keyword)) {
			return "";
		}
		
		HttpSession session = request.getSession(true);
		SessionModel sessionModel = SessionModel.getInstance(session);
		
		Long userId = sessionModel.getUserId();
		Long keeBookId = sessionModel.getKeeBookId();
		
		List<String> items = getPasswordDao().getSuggestKeywords(userId, keeBookId, keyword);
		StringBuilder sb = new StringBuilder();
		boolean empty = true;
		for (String s : items) {
			if (!empty) {
				sb.append("\n");
			}
			sb.append(s);
			empty = false;
		}
		
		return sb.toString();
	}

	public PasswordDao getPasswordDao() {
		return passwordDao;
	}

	public void setPasswordDao(PasswordDao passwordDao) {
		this.passwordDao = passwordDao;
	}
}
